/**
 * @version      BOSSCMS-Free
 * @trademark    BOSSCMS
 * @copyright    Copyright © by BOSSCMS-Free (https://www.bosscms.net/) 保留所有权利
 * @license      https://gitee.com/Greenpeas/BOSSCMS/license/ 和 https://www.bosscms.net/freelicense/
 * @link         https://www.bosscms.net/
 * @author       温州软界网络技术有限公司
**/
$(document).ready(function(e){
	id = $.request('id');
	parent = $.request('parent');
	function dir(folder,id,parent){
		return $.mpf('items','items','folder',{'folder':folder,'id':id,'parent':parent,'jsonmsg':'true'});
	}
	function getdir(folder, i, obj){
		folders = folder + (i==0?'':i);
		$.get(dir(folders,id,parent),function(data){
			if(data || $('input[name^="folder"][value="'+folders+'"]').length>0){
				getdir(folder, i+1, obj);
			}else{
				obj.val(folders).attr('value',folders).attr('required','required');
			}
		});
	}
	$(document).on('change','input[name^="folder"]',function(){
		the = $(this);
		$.getJSON(dir($(this).val(),id,parent),function(data){
			if(data){
				_alert(data.msg);
				the.focus();
			}
		})
	});

	$('textarea[name=type]').change(function(){
		bosscms = 1;
		type = $(this).val();
		if(type==9){
			$('[link]').attr('hide','hide');
			$('[unlink]').removeAttr('hide');
			$('input[name="folder"]').removeAttr('required');
		}else{
			$('[link]').removeAttr('hide');
			$('[unlink]').attr('hide','hide');
			$('input[name="folder"]').attr('required','required');
		}
		$('dl[theme]').attr('hide','hide');
		$('dl[type'+$(this).val()+']').removeAttr('hide');
		dls = $('dl[theme][type'+$('textarea[name=type]').val()+']').eq(0).find('textarea');
		folder = dls.attr('folder');
		if(folder){
			if(folder){
				getdir(folder, 0, $('input[name="folder"]'));
			}
		}else{
			$('input[name="folder"]').val('');
		}
		if(type.match(/^(2|3|4|5)$/)){
			$('dl[contribute],dl[comments]').attr('hide','hide');
			$('dl[contribute*=",'+type+',"],dl[comments*=",'+type+',"]').removeAttr('hide');
		}else{
			$('dl[contribute],dl[comments]').attr('hide','hide');
		}
	});

	$(document).on('change','td>code.select>textarea[name^=type]',function(){
		type = $(this).val();
		parent = $('.head>code.select>textarea[name="parent"]').val();
		if(type==9){
			$(this).parents('tr').find('td.fl').addClass('on').find('input').removeAttr('required');
			$(this).parents('tr').find('td.fl').find('input[name^="link"]').attr('required','required');
		}else{
			$(this).parents('tr').find('td.fl').removeClass('on').find('input').removeAttr('required');
			$(this).parents('tr').find('td.fl').find('input[name^="folder"]').attr('required','required');
			getdir(models[type], 0, $(this).parents('tr').find('input[name^="folder"]'));
		}
	}).on('change','.head>code.select>textarea[name="parent"]',function(){
		window.location.href = $.params(window.location.href,'parent',$(this).val());
	});
	
	$('.add.batch.button').click(function(){
		$('.main table tbody tr').last().after( $('table[hidden] tbody').html().replace(/{Rep}/g,Math.ceil(Math.random()*100000)) );
	}).click();
});